/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package accessor;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import model.Model;

/**
 *
 * @author Thanhnd
 */
public class CategoryDAO extends Model {

    public int insertCategory(Category category) {
        int retVal = 0;
        Connection conn = null;
        CallableStatement callable = null;
        try {
            conn = getConnection();
            callable = conn.prepareCall("{ call procCategory_add(?,?,?) }");
            callable.setString(1, category.getCategoryName());
            callable.setInt(2, category.getParent_id());
            callable.setInt(3, category.getDepth());
            retVal = callable.executeUpdate();
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(CategoryDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(CategoryDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(CategoryDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(CategoryDAO.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                callable.close();
                conn.close();
            } catch (SQLException ex) {
                Logger.getLogger(CategoryDAO.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return retVal;
    }

    public int updateCategory(Category category) {
        int retVal = 0;
        Connection conn = null;
        CallableStatement callable = null;
        try {
            conn = getConnection();
            callable = conn.prepareCall("{ call procCategory_update(?,?,?,?) }");
            callable.setInt(1, category.getCategoryID());
            callable.setString(2, category.getCategoryName());
            callable.setInt(3, category.getParent_id());
            callable.setInt(4, category.getDepth());
            retVal = callable.executeUpdate();
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(CategoryDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(CategoryDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(CategoryDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(CategoryDAO.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                callable.close();
                conn.close();
            } catch (SQLException ex) {
                Logger.getLogger(CategoryDAO.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return retVal;
    }

    public int deleteCategory(Category category) {
        int retVal = 0;
        Connection conn = null;
        CallableStatement callable = null;
        try {
            conn = getConnection();
            callable = conn.prepareCall("{ call procCategory_delete(?) }");
            callable.setInt(1, category.getCategoryID());
            retVal = callable.executeUpdate();
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(CategoryDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(CategoryDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(CategoryDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(CategoryDAO.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                callable.close();
                conn.close();
            } catch (SQLException ex) {
                Logger.getLogger(CategoryDAO.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return retVal;
    }

    public List<Category> getAllCategory() {
        List<Category> list = new ArrayList<Category>();
        ResultSet rs = null;
        Connection conn = null;
        CallableStatement callable = null;
        try {
            conn = getConnection();
            callable = conn.prepareCall("{ call procCategory_getall() }");
            rs = callable.executeQuery();
            while (rs.next()) {
                Category category = new Category();
                category.setCategoryID(rs.getInt(1));
                category.setCategoryName(rs.getString(2));
                category.setParent_id(rs.getInt(3));
                category.setDepth(rs.getInt(4));
                list.add(category);
            }
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(CategoryDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(CategoryDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(CategoryDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(CategoryDAO.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                rs.close();
                callable.close();
                conn.close();
            } catch (SQLException ex) {
                Logger.getLogger(CategoryDAO.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return list;
    }
}
